﻿using Aspose.Cells;
using FRMS.DAL;
using FRMS.Model;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class ExcelTools
    {
        public static string UploadFileToServer(IFormFile file, string webRootPath)
        {
            string result = string.Empty;
            try
            {
                String Tpath = "/File/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
                string name = file.FileName;
                string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string FilePath = webRootPath + Tpath;

                string type = Path.GetExtension(name);
                DirectoryInfo di = new DirectoryInfo(FilePath);

                if (!di.Exists) { di.Create(); }

                using (FileStream fs = File.Create(FilePath + FileName + type))
                {
                    // 复制文件
                    file.CopyTo(fs);
                    fs.Flush();
                    fs.Close();
                }
                result = FilePath + FileName + type;

            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "文件导入";
                dataClearBUS.AddErrorLog(errorLog);
                result = "文件导入异常";
            }

            return result;
        }
        /*  public static DataTable ImportCompareMeanExcel(string strFilePath, out string name, out string code)
          {
              Workbook workbook = new Workbook(strFilePath);
              DataTable dtExcel = null;
              try
              {
                  name = workbook.Worksheets[0].Name;
                  Cells cells = workbook.Worksheets[0].Cells;
                  dtExcel = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxColumn + 1, false);
                  try
                  {
                      code = cells.GetCell(99, 25).Value.ToString();
                  }
                  catch
                  {
                      code = "";
                  }
              }
              catch (Exception ex)
              {
                  throw new Exception(ex.Message);
              }
              return dtExcel;
          }*/

        /*public static ResponseMessage ImportBuildToData(DataTable dt, int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            string addfailinfo = string.Empty;
            string failinfo = string.Empty;
            try
            {
                if (dt.Rows[0][0].ToString().Trim() != "单元楼栋名称")
                {
                    message.errcode = 1;
                    message.errmsg = "单元格名称核验失败";
                }
                else
                {
                    int i = 1;
                    while (!string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim()))
                    {
                        BuildBUS bUS = new BuildBUS();
                        BuildInfo info = new BuildInfo();
                        info.BuildingName = dt.Rows[i][0].ToString().Trim();
                        info.projectID = projectid;
                        ResponseMessage result = bUS.AddBuild(info);
                        if (result.errcode == 1)
                        {
                            addfailinfo += info.BuildingName + ",";
                        }
                        i++;
                    }
                    if (addfailinfo.Length > 0)
                    {
                        message.errcode = 1;
                        message.errmsg =
                        failinfo += "楼栋单元" + addfailinfo + "导入失败";
                    }
                    else
                    {
                        message.errcode = 0;
                    }
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "导入单元楼栋";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "导入异常";
            }
            return message;
        }*/

        public static DataTable ImportCompareMeanExcel(string strFilePath, int index)
        {
            Workbook workbook = new Workbook(strFilePath);
            DataTable dtExcel = null;
            try
            {
                Cells cells = workbook.Worksheets[index].Cells;
                dtExcel = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxColumn + 1, false);
            }
            catch (Exception ex)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = ex.ToString();
                errorLog.Operation = "数据导入";
                dataClearBUS.AddErrorLog(errorLog);
                throw new Exception(ex.Message);
            }
            return dtExcel;
        }

        public static ResponseMessage ImportBuild(DataTable dt, int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            string addfailinfo = string.Empty;
            string failinfo = string.Empty;
            try
            {
                int count = dt.Rows.Count;
                for (int i = 1; i < count; i++)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim()))
                    {
                        BuildBUS bUS = new BuildBUS();
                        BuildInfo info = new BuildInfo();
                        info.BuildingName = dt.Rows[i][0].ToString().Trim();
                        info.projectID = projectid;
                        ResponseMessage result = bUS.AddBuild(info);
                        if (result.errcode == 1)
                        {
                            addfailinfo += info.BuildingName + ",";
                        }
                    }
                }
                if (addfailinfo.Length > 0)
                {
                    message.errcode = 1;
                    message.errmsg =
                    failinfo += "楼栋单元" + addfailinfo + "导入失败";
                }
                else
                {
                    message.errcode = 0;
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "文件导入";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "楼栋单元导入异常";
            }
            return message;
        }

        public static ResponseMessage ImportRoom(DataTable dt, int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            string addfailinfo = string.Empty;
            string failinfo = string.Empty;
            try
            {
                int count = dt.Rows.Count;
                for (int i = 1; i < count; i++)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim()))
                    {
                        RoomBUS bUS = new RoomBUS();
                        RoomInfo info = new RoomInfo();
                        info.RoomName = dt.Rows[i][0].ToString().Trim();
                        info.BuildingName = dt.Rows[i][1].ToString().Trim();
                        info.projectID = projectid;
                        ResponseMessage result = bUS.ImportRoom(info);
                        if (result.errcode == 1)
                        {
                            addfailinfo += info.BuildingName + ",";
                        }
                    }
                }
                if (addfailinfo.Length > 0)
                {
                    message.errcode = 1;
                    message.errmsg =
                    failinfo += "房间" + addfailinfo + "导入失败";
                }
                else
                {
                    message.errcode = 0;
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "房间导入";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "房间导入异常";
            }
            return message;
        }

        public static ResponseMessage ImportElevator(DataTable dt, int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            string addfailinfo = string.Empty;
            string failinfo = string.Empty;
            try
            {
                int count = dt.Rows.Count;
                for (int i = 1; i < count; i++)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim()))
                    {
                        ElevatorBUS bUS = new ElevatorBUS();
                        ElevatorInfo info = new ElevatorInfo();
                        info.ElevatorName = dt.Rows[i][0].ToString().Trim();
                        info.BuildingName = dt.Rows[i][1].ToString().Trim();
                        info.FloorInfo = dt.Rows[i][2].ToString().Trim();
                        info.pFloorInfo = dt.Rows[i][3].ToString().Trim();
                        info.Workmode = Convert.ToInt32(dt.Rows[i][4].ToString().Trim());
                        info.projectID = projectid;
                        ResponseMessage result = bUS.ImportElevator(info);
                        if (result.errcode == 1)
                        {
                            addfailinfo += info.ElevatorName + ",";
                        }
                    }
                }
                if (addfailinfo.Length > 0)
                {
                    message.errcode = 1;
                    message.errmsg =
                    failinfo += "电梯" + addfailinfo + "导入失败";
                }
                else
                {
                    message.errcode = 0;
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "电梯导入";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "电梯导入异常";
            }
            return message;
        }

        public static ResponseMessage ImportDevice(DataTable dt, int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            string addfailinfo = string.Empty;
            string failinfo = string.Empty;
            try
            {
                int count = dt.Rows.Count;
                for (int i = 1; i < count; i++)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim()))
                    {
                        DeviceBUS bUS = new DeviceBUS();
                        DeviceInfo info = new DeviceInfo();
                        info.DeviceName = dt.Rows[i][0].ToString().Trim();
                        info.DeviceIP = dt.Rows[i][1].ToString().Trim();
                        info.ElevatorName = dt.Rows[i][2].ToString().Trim();
                        info.projectID = projectid;
                        ResponseMessage result = bUS.ImportDevice(info);
                        if (result.errcode == 1)
                        {
                            addfailinfo += info.DeviceName + ",";
                        }
                    }
                }
                if (addfailinfo.Length > 0)
                {
                    message.errcode = 1;
                    message.errmsg =
                    failinfo += "设备" + addfailinfo + "导入失败";
                }
                else
                {
                    message.errcode = 0;
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "业主导入";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "业主导入异常";
            }
            return message;
        }

        public static ResponseMessage ImportOwner(DataTable dt, int projectid,string webRootPath)
        {
            ResponseMessage message = new ResponseMessage();
            string addfailinfo = string.Empty;
            string failinfo = string.Empty;
            DeviceDAL deviceDAL = new DeviceDAL();
            DeviceInfo deviceInfo = new DeviceInfo();
            BuildDAL buildDAL = new BuildDAL();
            BuildInfo buildInfo = new BuildInfo();
            try
            {
                deviceInfo.projectID = projectid;
                List<DeviceInfo> list = deviceDAL.QueryDeviceList(deviceInfo);
                int count = dt.Rows.Count;
                for (int i = 1; i < count; i++)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim()))
                    {
                        OwnerBUS bUS = new OwnerBUS();
                        OwnerInfo info = new OwnerInfo();
                        info.OwnerName = dt.Rows[i][0].ToString().Trim();
                        info.CertificateNO = dt.Rows[i][1].ToString().Trim();
                        info.Phone = dt.Rows[i][2].ToString().Trim();
                        info.OwnerTypeName =dt.Rows[i][3].ToString().Trim();
                        info.BuildingName = dt.Rows[i][4].ToString().Trim();
                        info.RoomName = dt.Rows[i][5].ToString().Trim();
                        info.CardNO = dt.Rows[i][10].ToString().Trim();
                        info.projectID = projectid;
                        VoucherInfo voucherInfo = new VoucherInfo();
                        voucherInfo.projectID = projectid;
                        voucherInfo.DeviceName = dt.Rows[i][6].ToString().Trim();
                        DeviceInfo device = list.FirstOrDefault(t => t.DeviceName == voucherInfo.DeviceName);
                        voucherInfo.DeviceID = device.ID;
                        voucherInfo.FloorInfo = device.FloorInfo;
                        voucherInfo.pFloorInfo = device.pFloorInfo;
                        voucherInfo.AuthorizedFloor = dt.Rows[i][7].ToString().Trim();
                        voucherInfo.EffectiveStarttime = dt.Rows[i][8].ToString().Trim();
                        voucherInfo.EffectiveEndtime = dt.Rows[i][9].ToString().Trim();
                        ResponseMessage result = bUS.ImportOwner(info, voucherInfo, dt.Rows[i][11].ToString().Trim(), webRootPath);
                        if (result.errcode == 1)
                        {
                            addfailinfo += info.OwnerName + ",";
                        }
                    }
                }
                if (addfailinfo.Length > 0)
                {
                    message.errcode = 1;
                    message.errmsg =
                    failinfo += "业主" + addfailinfo + "导入失败";
                }
                else
                {
                    message.errcode = 0;
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "业主导入";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "业主导入异常";
            }
            return message;
        }

        public static int GetOwnerType(string typename)
        {
            int type = -1;
            switch (typename)
            {
                case "业主": type = 1; break;
                case "物业": type = 2; break;
                case "租客": type = 4; break;
                case "访客": type = 5; break;
                case "其他": type = 6; break;
            }
            return type;
        }

       
    }

}
